﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.ComponentModel;
using SyringeControl.WinForms;

namespace SyringeControl.ControlConfiguration
{
    [TestFixture]
    class Test_EventHandlerAdapterFactory
    {
        GenericEventHandlerAdapterFactory adapterFactory = new GenericEventHandlerAdapterFactory();
        IEventLogger genericHandler = new TraceEventLogger();

        [Test]
        public void Can_create_adapters_to_EventHandler()
        {
            var del1 = adapterFactory.BuildAdapter<EventHandler>(genericHandler);
            del1(this, EventArgs.Empty);
            var del2 = adapterFactory.BuildAdapter<PropertyChangedEventHandler>(genericHandler);
            del2(this, new PropertyChangedEventArgs("StringField"));
            var del3 = adapterFactory.BuildAdapter<EventHandler<EventArgs<string>>>(genericHandler);
            del3(this, new EventArgs<string>("hello world"));
        }

        [Test]
        public void Can_create_adapters_to_Action()
        {
            Assert.That(adapterFactory.CanBuild(typeof(Action)));
            var del1 = adapterFactory.BuildAdapter<Action>(genericHandler);
            del1();
            
        }

        [Test]
        public void Can_create_adapters_to_Action_with_parameters()
        {
            Assert.That(adapterFactory.CanBuild(typeof(Action<object>)));
            var del1 = adapterFactory.BuildAdapter<Action<object>>(genericHandler);
            del1(this);
            Assert.That(adapterFactory.CanBuild(typeof(Action<string>)));
            var del2 = adapterFactory.BuildAdapter<Action<string>>(genericHandler);
            del2("Hello");
        }

        [Test]
        [ExpectedException(typeof(NotSupportedException))]
        public void Throws_when_delegate_is_not_supported()
        {
            adapterFactory.BuildAdapter(typeof(Func<,,,,>), genericHandler);
        }
    }
}
